package com.semidata.trp.report.config;

import java.io.File;

import com.semidata.rtd.core.config.DefaultValue;
import com.semidata.rtd.core.config.DefaultValueClass;
import com.semidata.rtd.core.config.DefaultValueHandler;
import com.semidata.rtd.core.config.Handler;
import com.semidata.rtd.core.config.Property;
import com.semidata.rtd.core.config.UrlHandler;

public class Config extends com.semidata.rtd.core.config.Config {
    private static String DEFAULT_CONFIG_FILE = "conf/report.conf";
    private static Config config = null;
    
    public static class DefaultLogDir implements DefaultValueHandler {

        @Override
        public String value() {
            File file = new File(Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath());
            file = new File(file.getParentFile().getParentFile(), "logs/report");
            return file.getAbsolutePath();
        }

    }

    public static class DefaultDataDir implements DefaultValueHandler {

        @Override
        public String value() {
            File file = new File(Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath());
            file = new File(file.getParentFile().getParentFile(), "data/report");
            return file.getAbsolutePath();
        }

    }

    public static class DefaultReportDir implements DefaultValueHandler {

        @Override
        public String value() {
            File file = new File(Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath());
            file = new File(file.getParentFile().getParentFile(), "data/middledata");
            return file.getAbsolutePath();
        }

    }

    public static void init(String configFile) {
        if (configFile == null)
            configFile = Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath();
        config = new Config(configFile);
    }

    public static Config getConfig() {
        return config;
    }

    private Config(String configFile) {
        initConfig(configFile);
    }

    @Property("http.port")
    @DefaultValue("6200")
    private int httpPort;

    @Property("service.wait")
    @DefaultValue("60000")
    private long wait;

    @Property("service.round")
    @DefaultValue("1000")
    private long round;

    @Property("http.threads")
    @DefaultValue("4")
    private int httpThreads;

    @Property("report.threads")
    @DefaultValue("4")
    private int reportThreads;

    @Property("log.root")
    @DefaultValueClass(DefaultLogDir.class)
    private String logRoot;

    @Property("log.debug.enabled")
    @DefaultValue("false")
    private boolean logDebugEnabled;

    @Property("url.report.recompute")
    @DefaultValue("/recompute/")
    @Handler(UrlHandler.class)
    private String recomputeUrl;

    @Property("url.report.report")
    @DefaultValue("/report/")
    @Handler(UrlHandler.class)
    private String reportUrl;

    @Property("monitor.log.dir")
    @DefaultValueClass(DefaultDataDir.class)
    private String monitorLogDir;

    @Property("report.dir")
    @DefaultValueClass(DefaultReportDir.class)
    private String reportDir;

    @Property("report.tail")
    @DefaultValue(".log")
    private String reportTail;

    @Property("report.day")
    private int dayStart;

    public int getHttpPort() {
        return httpPort;
    }

    public long getWait() {
        return wait;
    }

    public long getRound() {
        return round;
    }

    public int getHttpThreads() {
        return httpThreads;
    }

    public int getReportThreads() {
        return reportThreads;
    }

    public String getLogRoot() {
        return logRoot;
    }

    public boolean isLogDebugEnabled() {
        return logDebugEnabled;
    }

    public String getRecomputeUrl() {
        return recomputeUrl;
    }

    public String getMonitorLogDir() {
        return monitorLogDir;
    }

    public String getReportDir() {
        return reportDir;
    }

    public String getReportTail() {
        return reportTail;
    }
        
    public int getDayStart(){
    	return dayStart;
    }
    
    @Override
    public String toString() {
        return "Config [httpPort=" + httpPort + ", wait=" + wait + ", httpThreads=" + httpThreads + ", logRoot=" + logRoot + ", logDebugEnabled=" + logDebugEnabled + ", recomputeUrl=" + recomputeUrl
                + ", monitorLogDir=" + monitorLogDir + ", dayStart=" + dayStart + "]";
    }

    public static void main(String[] args) {
        init(null);
        System.out.println(getConfig().toString());
    }

	public String getReportUrl() {
		return reportUrl;
	}

}
